#!/bin/bash

yum -y update 
yum -y install socat minicom net-tools wget java-1.8.0-openjdk.x86_64 python3-pip
sed -i 's/TLS1\.[0-1],\s//g' /etc/java/java-1.8.0-openjdk/java-1.8.0-openjdk-1.8.0.312.b07-10.p01.ky10.x86_64/lib/security/java.security
sed -i 's/TLS1\.[0-1],\s//g' /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-10.p01.ky10.x86_64/jre/lib/security/java.security
pip3 install requests -i https://mirrors.aliyun.com/pypi/simple/
pip3 install supervisor
echo "[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target" > /lib/systemd/system/supervisord.service
systemctl daemon-reload
systemctl enable supervisord
echo "[unix_http_server]
file=/var/run/supervisor.sock

[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/var/run/supervisord.pid
nodaemon=false
minfds=1024
minprocs=200

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

[include]
files = supervisord.d/*.ini

stopasgroup = true
killasgroup = true
" > /etc/supervisord.conf
mkdir /etc/supervisord.d
systemctl start supervisord
mkdir -p /data/{client,slave}
mkdir -p /root/logs/iot
echo "[program:iot-client]
user=root
command=java -jar /data/client/manage.jar -h 127.0.0.1 -p 10011 -d d1 -l DEBUG -br 115200
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/root/logs/iotp/client-out.log
stopasgroup=true
killasgroup=true" > /etc/supervisord.d/iot-client.ini
echo "[program:iot-slave]
user=root
command=java -Djava.net.preferIPv4Stack=true -jar /data/slave/slave.jar -t 10015 -l DEBUG -u admin -p 123456
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/root/logs/iotp/slave-out.log
stopasgroup=true
killasgroup=true" >  /etc/supervisord.d/iot-slave.ini
wget https://res.codedance.top/iotp/client/client-2.jar -O /data/client/client-2.jar
wget https://res.codedance.top/iotp/client/manage.jar -O /data/client/manage.jar
wget https://res.codedance.top/iotp/slave/slave.jar -O /data/slave/slave.jar
echo "ttyS1" > /data/client/com.cnf
echo "" > /data/client/init.ini
echo "[com]
comName.0 = ttyS2
dataBits.0 = 8
baudRate.0 = 9600
stopBits.0 = 1
parity.0 = 0

[register-coil]
register = 0-40100
coil = 0-400

[slave]
type.1 = rtu
com.1 = 0" > /data/slave/slave.ini
supervisorctl reload